<?php

/**
 * 快捷获取登录用户信息
 * 
 * @param  string $key     用户表的字段
 * @param  mixed  $default 获取不到时的默认值 
 * @return mixed
 */
function u($key = '', $default = null)
{
    try {
        if ($key) {
            return isset(Yii::$app->user->identity->$key) ? Yii::$app->user->identity->$key : $default;
        } else {
            return Yii::$app->user->identity ?: new frontend\components\WebUser;
        }
    } catch (\Exception $e) {
        if ($key) {
            return null;
        } else {
            return new frontend\components\WebUser;
        }
    }
}

/**
 * 快捷获取 `user` 组件信息
 */
function user()
{
    return Yii::$app->user;
}

/**
 * 快捷获取 `session` 组件信息，使用方式如下：
 * session('key')  <=>  Yii::$app->session->get('key');
 * session('key', 'value')  <=>  Yii::$app->session->set('key', 'value');
 * session('key', 'value', 3600)  <=>  Yii::$app->session->set('key', 'value', 3600);
 * session('key', null)  <=>  Yii::$app->session->remove('key');
 */
function session()
{
    switch (func_num_args()) {
        case 0:
            return Yii::$app->session;
        case 1:
            return Yii::$app->session->get(func_get_arg(0));
        case 2:
            if (func_get_arg(1) === null) {
                return Yii::$app->session->remove(func_get_arg(0));
            }
        case 3:
            call_user_func_array([Yii::$app->session, 'set'], func_get_args());
            break;
    }
}

/**
 * 快捷使用 `cookie` 助手方法，使用方式如下：
 * cookie('key')  <=>  Cookie::get('key');
 * cookie('key', null)  <=>  Cookie::remove('key');
 * cookie('key', 'value')  <=>  Cookie::set('key', 'value');
 * cookie('key', 'value', 3600)  <=>  Cookie::set('key', 'value', 3600);
 */
function cookie()
{
    switch (func_num_args()) {
        case 0:
            return Yii::$app->response->cookies;
        case 1:
            return common\helpers\Cookie::get(func_get_arg(0));
        case 2:
            if (func_get_arg(1) === null) {
                return common\helpers\Cookie::remove(func_get_arg(0));
            }
        case 3:
            call_user_func_array(['common\helpers\Cookie', 'set'], func_get_args());
            break;
    }
}

/**
 * 快捷调用 `request` 组件信息，优先获取post中的参数
 * 
 * @param  string $key     参数名
 * @param  mixed  $default get和post中都获取不到时的默认值
 * @return mixed
 */
function req($key = '', $default = null)
{
    if ($key) {
        if (Yii::$app->request->post($key) === null) {
            return Yii::$app->request->get($key, $default);
        } else {
            return Yii::$app->request->post($key);
        }
    } else {
        return Yii::$app->request;
    }
}

/**
 * 快捷post中的参数，参数不填时获取所有post参数
 * 
 * @param  string $key     参数名
 * @param  mixed  $default 获取不到时的默认值
 * @return mixed
 */
function post($key = '', $default = null)
{
    if ($key) {
        return Yii::$app->request->post($key, $default);
    } else {
        return Yii::$app->request->post();
    }
}

/**
 * 快捷get中的参数，参数不填时获取所有get参数
 * 
 * @param  string $key     参数名
 * @param  mixed  $default 获取不到时的默认值
 * @return mixed
 */
function get($key = '', $default = null)
{
    if ($key) {
        return Yii::$app->request->get($key, $default);
    } else {
        return Yii::$app->request->get();
    }
}

/**
 * 快捷调用 `response` 组件，默认使用html方式输出
 */
function res()
{
    switch (func_num_args()) {
        case 0:
            return Yii::$app->response;
        case 1:
            if (req()->isAjax) {
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            } else {
                Yii::$app->response->format = \yii\web\Response::FORMAT_HTML;
            }
            Yii::$app->response->data = func_get_arg(0);
            Yii::$app->response->send();
            return Yii::$app->response;
    }
}

/**
 * 快捷调用 `cache` 组件信息，使用方式如下：
 * cache('key')  <=>  Yii::$app->cache->get('key');
 * cache('key', 'value')  <=>  Yii::$app->cache->set('key', 'value');
 * cache('key', 'value', 3600)  <=>  Yii::$app->cache->set('key', 'value', 3600);
 * cache('key', 'value', 3600, $dependency)  <=>  Yii::$app->cache->set('key', 'value', 3600, $dependency);
 * cache('key', null)  <=>  Yii::$app->cache->delete('key');
 */
function cache()
{
    switch (func_num_args()) {
        case 0:
            return Yii::$app->cache;
        case 1:
            return Yii::$app->cache->get(func_get_arg(0));
        case 2:
            if (func_get_arg(1) === null) {
                return Yii::$app->cache->delete(func_get_arg(0));
            }
        case 3:
        case 4:
            call_user_func_array([Yii::$app->cache, 'set'], func_get_args());
            break;
    }
}

/**
 * 快捷获取 `redis` 组件信息
 */
function redis()
{
    return Yii::$app->redis;
}

/**
 * 快捷调用配置信息
 *
 * @param  string $key     配置名
 * @param  mixed  $default 配置不存在时的默认值
 * @return mixed
 */
function config($key = '', $default = null)
{
    $config = new common\components\Config;
    if ($key) {
        return $config->get($key, $default);
    } else {
        return $config;
    }
}

/**
 * 快捷支付回调地址
 *
 * @param  string $key     配置名
 * @param  mixed  $default 配置不存在时的默认值
 * @return mixed
 */
function zynotify()
{
    $data = $_REQUEST;
    // $data = [
    //         'Sjt_MerchantID' => '724',
    //         'Sjt_UserName' => '',
    //         'Sjt_TransID' => '100001201711081439008134',
    //         'Sjt_Return' => '1',
    //         'Sjt_Error' => '',
    //         'Sjt_factMoney' => '0.000',
    //         'Sjt_SuccTime' => '',
    //         'Sjt_BType' => '2',
    //         'Sjt_Sign' => 'c57203c66518beee4533a62cb41c1f80',
    //     ];
    if (isset($data['Sjt_Return']) && $data['Sjt_Return'] == '1') {
        $sign = $data['Sjt_Sign'];
        unset($data['Sjt_Sign']);
        $str = '';
        foreach ($data as $key => $value) {
            $str .= "{$value}";
        }

        $newSign = md5($str . EASY_MDKEY);
        // test($newSign, $sign . EASY_MDKEY));
        if ($sign == $newSign) {
            $userCharge = common\models\UserCharge::find()->where('trade_no = :trade_no', [':trade_no' => $data['Sjt_TransID']])->one();
            //有这笔订单
            if (!empty($userCharge)) {
                // $tradeAmount = $data['ubomoney'];
                if ($userCharge->charge_state == 1) {
                    $user = common\models\User::findOne($userCharge->user_id);
                    $user->account += $userCharge->amount  * 0.992;
                    if ($user->save()) {
                        $userCharge->charge_state = 2;
                    }
                }
                $userCharge->update();
                echo 'OK'; die;
            }
            exit('OK');
        }
    }
    exit('fail');
}

/**
 * 快捷支付回调地址
 *
 * @param  string $key     配置名
 * @param  mixed  $default 配置不存在时的默认值
 * @return mixed
 */
function winotify()
{
    $data = $_POST;
    // $data =  [
    //         'Amount' => '100',
    //         'BillNo' => '100001201711131726075351',
    //         'FlowId' => '2017111321001104585288584285',
    //         'Remark' => '',
    //         'Status' => '1',
    //         'MD5info' => 'ad5c680a42157c9c10de651201de2015',
    //         'TransTime' => '20171113172615',
    //     ];
    if (isset($data['Status']) && $data['Status'] == '1') {

            $userCharge = common\models\UserCharge::find()->where('trade_no = :trade_no', [':trade_no' => $data['BillNo']])->one();
            if (!empty($userCharge)) {
                if($userCharge->md5 == $data['MD5info']) {
                        if ($userCharge->charge_state == 1) {
                            $user = common\models\User::findOne($userCharge->user_id);
                            $user->account += $userCharge->amount  * 0.992;
                            if ($user->save()) {
                                $userCharge->charge_state = 2;
                            }
                        }
                        $userCharge->update();
                        echo 'SUCCESS'; die;
                    }
            }
    }
    exit('fail');
}


/**
 * 快捷支付回调地址
 *
 * @param  string $key     配置名
 * @param  mixed  $default 配置不存在时的默认值
 * @return mixed
 */
function newsnotify()
{
    // $data = $_POST;
    // l($data);
    // $data =   [
    //         'errcode' => '0',
    //         'msg' => '支付成功',
    //         'oid' => 'ad6d117cb3c2ae214fcb529e05ebb498',
    //         'ord_no' => '100001201711151757424656',
    //         'pmt_id' => '2014111414',
    //         'ord_type' => '1',
    //         'original_amount' => '1',
    //         'trade_amount' => '1',
    //         'status' => '1',
    //         'pay_type' => '2',
    //         'return_status' => '2',
    //         'addtime' => '2017-11-15 17:57:44',
    //         'uptime' => '2017-11-15 17:57:53',
    //         'notify_url' => 'http://www.megaboard.com.cn/site/newsnotify',
    //         'sign' => '09c5b9bd137b91e69f4fac0c86ddc361',
    //     ];
    $data['pmt_id'] = $_POST['pmt_id'];
    $data['ord_no'] = $_POST['ord_no'];
    $data['trade_amount'] = $_POST['trade_amount'];
    $newSign = signs($data, CASUAL_NEW_KEY);

    if (isset($_POST['status']) && $_POST['status'] == '1') {
        if($newSign == $_POST['sign']) {
            $userCharge = common\models\UserCharge::find()->where('trade_no = :trade_no', [':trade_no' => $data['ord_no']])->one();
            if (!empty($userCharge)) {
                if($userCharge->amount  == $data['trade_amount'] / 100) {
                        if ($userCharge->charge_state == 1) {
                            $user = common\models\User::findOne($userCharge->user_id);
                            $user->account += $userCharge->amount  * 0.992;
                            if ($user->save()) {
                                $userCharge->charge_state = 2;
                            }
                        }
                        $userCharge->update();
                        echo 'SUCCESS'; die;
                    }
            }
        }
    }
    exit('fail');
}

/**
 * 快捷调用option表参数，使用方式如下：
 * option()  =>  获得所有系统参数
 * option('key')  =>  获得指定系统参数
 * option('key', 'value')  =>  设置指定系统参数
 */
function option($key = '', $value = null)
{
    static $options = null;
    $argsNum = func_num_args();
    if ($argsNum <= 1 && $options === null) {
        $options = common\models\Option::find()->where(['type' => common\models\Option::TYPE_SYSTEM])->asArray()->map('option_name', 'option_value');
    }
    switch ($argsNum) {
        case 0:
            return $options;
        case 1:
            return isset($options[$key]) ? unserialize($options[$key]) : null;
        case 2:
            if (!($option = common\models\Option::find()->where(['option_name' => $key, 'type' => common\models\Option::TYPE_SYSTEM])->one())) {
                $option = new common\models\Option;
                $option->option_name = $key;
                $option->type = common\models\Option::TYPE_SYSTEM;
            }
            $option->option_value = serialize($value);
            $option->save(false);
    }
}

/**
 * 快捷调用 Ajax 请求的成功返回
 * 
 * @see common\components\WebController::success()
 */
function success()
{
    return call_user_func_array(['common\components\WebController', 'success'], func_get_args());
}

/**
 * 快捷调用 Ajax 请求的失败返回
 * 
 * @see common\components\WebController::error()
 */
function error()
{
    return call_user_func_array(['common\components\WebController', 'error'], func_get_args());
}

/**
 * 快捷调用 jsonp 请求的返回
 * 
 * @see common\components\WebController::jsonp()
 */
function jsonp()
{
    return call_user_func_array(['common\components\WebController', 'jsonp'], func_get_args());
}

/**
 * 快捷调用抛出用户异常方法
 * 
 * @see common\traits\FuncTrait::throwHttpException()
 */
function throwex()
{
    call_user_func_array(['common\traits\FuncTrait', 'throwHttpException'], func_get_args());
}

/**
 * 快捷调用生成链接方法，不填参数表示直接获取当前链接
 * 
 * @see common\traits\FuncTrait::currentUrl()
 * @see common\traits\FuncTrait::createUrl()
 */
function url()
{
    if (func_num_args() === 0) {
        return call_user_func_array(['common\traits\FuncTrait', 'currentUrl'], func_get_args());
    } else {
        return call_user_func_array(['common\traits\FuncTrait', 'createUrl'], func_get_args());
    }
}

/**
 * 快捷记录信息到文件
 * 
 * @see common\traits\FuncTrait::log()
 */
function l()
{
    call_user_func_array(['common\traits\FuncTrait', 'log'], func_get_args());
}

/**
 * 快捷调用公众号配置信息
 *
 * @return object
 */
function wechatInfo($url = '')
{
    if (empty($url)) {
        $url = $_SERVER['HTTP_HOST'];
    }
    $wechat = new common\models\RingWechat;
    $data = $wechat::find()->where(['url' => $url])->one();
    if (!empty($data)) {
        return (object)$data->attributes;
    }
    $wechat = new common\models\RingWechat;
    return (object)$wechat::find()->one()->attributes;
}


function signs($array, $keys=null){
    $signature = array();
    foreach($array as $key=>$value){
        $signature[$key]=$key.'='.$value;
    }
    $signature['open_key']='open_key'.'=' . $keys;
    ksort($signature);
    #先sha1加密 在md5加密
    $sign_str = md5(sha1(implode('&', $signature)));
    return $sign_str;
    }

function http_post_data($url, $data_string) {  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_POST, 1);  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(  
        "Content-Type: application/json; charset=utf-8",  
        "Content-Length: " . strlen($data_string))  
    );  
    ob_start();  
    curl_exec($ch);  
    $return_content = ob_get_contents();  
    ob_end_clean();  
    $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
    return array($return_code, $return_content);  
    } 

//解密流程函数
function PrivateDecrypt($encrypted,$pi_key){
    $crypto = '';
    foreach (str_split(urlsafe_b64decode($encrypted), 128) as $chunk) {

        openssl_private_decrypt($chunk, $decryptData, $pi_key);

        $crypto .= $decryptData;

    }
    return $crypto;
}
//解密函数
 function urlsafe_b64decode($string) {
                    $data = str_replace(array('-','_'),array('+','/'),$string);
                    $mod4 = strlen($data) % 4;
                    if ($mod4) {
                        $data .= substr('====', $mod4);
                    }
                    return base64_decode($data);
}

